package com.njwb.day15.zqzb.jdbc03;

import com.njwb.day15.zqzb.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class DeleteStudent {
    public static void detele() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生编号：");
        int sid = scanner.nextInt();
        //构建连接对象
        Connection connection= null ;
        //命令器
        PreparedStatement pstmt= null ;
        try {
            connection = DbUtil.getConnection();
            //3、创建命令器
            pstmt = connection.prepareStatement("delete from tb_student where sid =?");
            pstmt.setInt(1,sid);
            //5.执行sql语句
            int result = pstmt.executeUpdate();
            if(result>0) {
                System.out.println("数据删除成功！！！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
           DbUtil.closeConnection(null,pstmt,connection);
        }
    }
}
